package com.lql.common.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Longge
 */

public class RequestUtil {

	/**
	 *返回一个上文的url
	 * */
	public static String getReferer(HttpServletRequest request){
		return request.getHeader("Referer");
	}
	
	/**
	 *返回一个上文的url,并去掉域名和项目名
	 * */
	public static String getLocalReferer(HttpServletRequest request){
		String url=RequestUtil.getReferer(request);
		return url.replaceAll((request.getScheme()+"://"+request.getServerName()+request.getContextPath()).trim(), "");
	}

	/**
	 *返回一个上文的url,并去掉域名和项目名将字符串编码转换为GBK
	 * */
	public static String getLocalRefererGBK(HttpServletRequest request){
		String url=RequestUtil.getReferer(request);
		return url.replaceAll((request.getScheme()+"://"+request.getServerName()+request.getContextPath()).trim(), "");
		
	}

	/**
	 * 返回一个本页面(当前页面)带参数的url
	 * */
	public static String getReUrl(HttpServletRequest request){
		String url=request.getRequestURI(); 
		if(request.getQueryString()!=null) 
		url+="?"+request.getQueryString(); 
		return url;
	}
	
	/**
	 * 返回一个本页面(当前页面)带不参数的url,<br>
	 * 并去掉request.getContextPath()项目名称
	 * */
	public static String getLocalReUrl(HttpServletRequest request){
		return getReUrl(request).replaceAll(request.getContextPath(), "");
	}
		
	/**
	 * 封装了request.getParameter(String args)<br>
	 * 并捕捉异常，防止异常抛出<br>
	 * @param request
	 * @param key 参数名称
	 * @return String
	 * @throws NullPointerException 抛出返回 ""  <br>
	 * 
	 */
	public static String getParameter(String key, HttpServletRequest request){
		try{
			String value=request.getParameter(key).trim();
			return value;
		}catch(NullPointerException e){
			return "";
		}
	}
	
	/**
	 * 封装了request.getParameter(String args)<br>
	 * 并捕捉异常，防止异常抛出<br>	 * 
	 * @param request
	 * @param key 参数名称
	 * @return String
	 * @throws NullPointerException  抛出返回: ""  <br>
	 * @throws UnsupportedEncodingException 抛出返回: ""  <br>
	 */
	public static String getParameterGBK(String key, HttpServletRequest request){
		try{
			String value=request.getParameter(key).trim();
			value=new String(value.getBytes("ISO-8859-1"),"GBK");
			return value;
		}catch(NullPointerException e){
			return "";
		}catch(UnsupportedEncodingException ue){
			return "";
		}
	}
	
	/**
	 * 封装了request.getParameter(String args)<br>
	 * 并捕捉异常，防止异常抛出<br>
	 * 把value转成数字，并捕捉是否是数字异常<br>
	 * @param request
	 * @param key 参数名称
	 * @return int
	 * @throws NumberFormatException 抛出返回: 0
	 */
	public static int getParameterNumberFormat(String key, HttpServletRequest request){
		try{
			int value=Integer.parseInt(RequestUtil.getParameter(key, request));
			return value;
		}catch(NumberFormatException e){
			return 0; 
		}
	}
	
	/**
	 * 封装了request.getParameter(String args)<br>
	 * 并捕捉异常，防止异常抛出<br>
	 * 把value转成Double，并捕捉是否是Double异常<br>
	 * @param request
	 * @param key 参数名称
	 * @return int
	 * @throws NumberFormatException 抛出返回: 0
	 */
	public static double getParameterDoubleFormat(String key, HttpServletRequest request){
		try{
			Double value=Double.parseDouble(RequestUtil.getParameter(key, request));
			if(value!=null){
				return value;
			}else{
				return 0.00;
			}
		}catch(Exception e){
			return 0.00; 
		}
	}
	
	/**
	 * 封装了request.getParameter(String args)<br>
	 * 并捕捉异常，防止异常抛出<br>
	 * 把value转成Float，并捕捉是否是Float异常<br>
	 * @param request
	 * @param key 参数名称
	 * @return int
	 * @throws NumberFormatException 抛出返回: 0
	 */
	public static Float getParameterFloatFormat(String key, HttpServletRequest request){
		try{
			Float value=Float.parseFloat(RequestUtil.getParameter(key, request));
			if(value!=null){
				return value;
			}else{
				return 0.00f;
			}
		}catch(Exception e){
			return 0.00f; 
		}
	}
	
	
	/**
	 * 封装了request.getParameter(String args)<br>
	 * 并捕捉异常，防止异常抛出<br>
	 * 把value转成Long，并捕捉是否是数字异常<br>
	 * @param request
	 * @param key 参数名称
	 * @return int
	 * @throws NumberFormatException 抛出返回: 0
	 */
	public static long getParameterLongFormat(String key, HttpServletRequest request){
		try{
			Long value=Long.parseLong(RequestUtil.getParameter(key, request));
			if(value!=null){
				return value;
			}else{
				return 0;
			}
		}catch(Exception e){
			return 0; 
		}
	}
	/**
	 * 封装了request.getParameter(String args)<br>
	 * 并捕捉异常，防止异常抛出<br>
	 * 把value转成Long，并捕捉是否是数字异常<br>
	 * @param request
	 * @param key 参数名称
	 * @return int
	 * @throws NumberFormatException 抛出返回: 0
	 */
	/*public static String [] getParameterValues(String key, HttpServletRequest request){
		String [] s={"0"};
		try{
			String [] value=request.getParameterValues(key);
			if(value!=null){
				return value;
			}else{
				return  s;
			}
		}catch(Exception e){
			return s; 
		}
	}*/
	
	/**
	 * 返回所有参数
	 */
	public static Map<String,String> getParameterMap(HttpServletRequest request){
		// 参数Map
	    Map properties = request.getParameterMap();
	    // 返回值Map
	    Map<String,String> returnMap = new HashMap();
	    Iterator entries = properties.entrySet().iterator();
	    Map.Entry entry;
	    String name = "";
	    String value = "";
	    while (entries.hasNext()) {
	        entry = (Map.Entry) entries.next();
	        name = (String) entry.getKey();
	        Object valueObj = entry.getValue();
	        if(null == valueObj){
	            value = "";
	        }else if(valueObj instanceof String[]){
	            String[] values = (String[])valueObj;
	            for(int i=0;i<values.length;i++){
	                value = values[i] + ",";
	            }
	            value = value.substring(0, value.length()-1);
	        }else{
	            value = valueObj.toString();
	        }
	        returnMap.put(name, value);
	    }
	    return returnMap;
	}
	
	/**
	 * 返回所有参数不包含空参数
	 */
	public static Map<String,String> getParameterRemoveNull(HttpServletRequest request){
		
		Map<String,String> map=getParameterMap(request);
		
		Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();;
		
		while (it.hasNext()) {
			
			Map.Entry<String, String> entry = it.next();
			
			if(StringUtils.isEmpty(entry.getValue())){
				it.remove();
			}
		}
		
	    return map;
	}
	
	/**
	 * 返回API的特定ID,商户编号
	 */
	public static int getParameterAPIMerNo(HttpServletRequest request){
		try{
			int value=Integer.parseInt(RequestUtil.getParameter("MerNo", request));
			return value-10000000;
		}catch(NumberFormatException e){
			return 0; 
		}
	}
	
	/**
	 * 获取订单号
	 */
	public static long getParameterOrderId(HttpServletRequest request){
		try{
			
			Object obj=request.getAttribute("orderId")!=null?request.getAttribute("orderId"):request.getParameter("orderId"); 
			
			return Long.parseLong(obj.toString());
			
			
		}catch(Exception e){
			return 0l; 
		}
	}
	
	/**
	 * 获取信用卡号
	 */
	public static String getParameterCardNo(HttpServletRequest request){
		try{
			
			Object obj=!StringUtils.isEmpty(request.getParameter("cardNo"))?request.getParameter("cardNo"):request.getParameter("PayCardNo");
			
			if(obj!=null) {
				return StringUtils.deleteWhitespace(obj.toString());
			}else {
				return "";
			}
			
		}catch(Exception e){
			return ""; 
		}
	}
	
	/**
	 * 获取信用卡号
	 */
	public static int getParameterPayMethod(HttpServletRequest request){
		
		return getParameterNumberFormat("payMethod", request)!=0?getParameterNumberFormat("payMethod", request):getParameterNumberFormat("PayMethod", request);
	}
	

	/**
	 * 获取手机号
	 */
	public static String getParameterTel(HttpServletRequest request){
		try{
			
			Object obj=!StringUtils.isEmpty(request.getParameter("tel"))?request.getParameter("tel"):request.getParameter("Tel");
			
			return obj.toString();
			
		}catch(Exception e){
			return ""; 
		}
	}
	
	/**
	 * 获取手机号
	 */
	public static String getParameterName(HttpServletRequest request){
		try{
			
			Object obj=!StringUtils.isEmpty(request.getParameter("name"))?request.getParameter("name"):request.getParameter("Name");
			
			return obj.toString();
			
		}catch(Exception e){
			return ""; 
		}
	}
	
	/**
	 * 获取手机号
	 */
	public static String getParameterCardId(HttpServletRequest request){
		try{
			
			Object obj=!StringUtils.isEmpty(request.getParameter("cardId"))?request.getParameter("cardId"):request.getParameter("CardId");
			
			return obj.toString();
			
		}catch(Exception e){
			return ""; 
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub		
	}

}
